using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Tomahawk.Runtime.Screen.Controls;

namespace Tomahawk.Runtime.Screen
{
    /// <summary>
    /// This class represent a common KeyFrame functionality in a control animation
    /// </summary>
    public abstract class ControlKeyFrame
    {

        protected ScreenControl control;
        public ScreenControl Control
        {
            get { return control; }
        }

        protected float startTime;
        public float StartTime
        {
            get { return startTime; }
        }

        protected float endTime;
        public float EndTime
        {
            get { return endTime; }
        }

        /// <summary>
        /// Constructor
        /// </summary>
        public ControlKeyFrame(ScreenControl controlToAnimate, float startTime, float endTime)
        {
            this.control = controlToAnimate;
            this.startTime = startTime;
            this.endTime = endTime;
        }

        bool animatedOnce = false;
        protected float localElapsedTime = 0;

        /// <summary>
        /// Reset the control
        /// </summary>
        public void Reset(float speed)
        {
            animatedOnce = false;
            if (speed >= 0)
                localElapsedTime = 0;
            else
                localElapsedTime = endTime;
        }

        /// <summary>
        /// Animate the control properties upon the elapsed time
        /// </summary>
        public void Animate(float time, float totalTime)
        {
            //if it's inside the interval, animate it!
            if ((totalTime > this.startTime) && (totalTime < this.endTime))
            {
                //local elapsed time
                localElapsedTime += time;

                //first animation frame
                if (!animatedOnce)
                {
                    animatedOnce = true;
                    this.onFirstFrame();
                }

                //animate this frame
                onAnimate(time);
            }
            else 
            {
                if (animatedOnce)
                {
                    animatedOnce = false;
                    this.onLastFrame(time);
                }
            }
        }

        /// <summary>
        /// Override this to provide the logic
        /// </summary>
        protected abstract void onAnimate(float time);

        /// <summary>
        /// Override this to provide first frame logic
        /// </summary>
        protected virtual void onFirstFrame()
        {
        }

        /// <summary>
        /// Override this to provide last frame logic
        /// </summary>
        protected virtual void onLastFrame(float time)
        {
        }

    }    

}


